package com.tool;

import cn.hutool.core.collection.ListUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

public class ToolClass {

    /**
     * 获取泛型参数类型Class
     *
     * @param obj
     * @return
     */
    public static Class getClassByThis(Object obj) {
        return getClassByThis(obj, 0);
    }

    public static Class getClassByThis(Object obj, int num) {
        Class clz = obj.getClass();
        Type type = clz.getGenericSuperclass();//获取泛型父类，必须用该方法，此处的泛型父类不是指当前的类，而是具体继承的BaseAction<Standard>，当前类为BaseAction<T>泛型尚未确定
        ParameterizedType pt = (ParameterizedType) type;//将泛型父类转换为具体的那种类型
        Class modelClass = (Class) pt.getActualTypeArguments()[num];
        return modelClass;
    }

    public static boolean isNumber(Class clazz) {
        return ListUtil.of(int.class, Integer.class, long.class, Long.class).contains(clazz);
    }

    public static boolean isDate(Class clazz) {
        return ListUtil.of(Date.class, LocalDateTime.class, LocalDate.class, LocalTime.class).contains(clazz);
    }
}
